Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Reflection
Imports System.windows.forms

Public Class PublicFunction

    Public Shared Function OpenSQLConnection() As Boolean
        PublicVariable.Connection = New SqlConnection
        PublicVariable.Connection.ConnectionString = PublicVariable.ConnectionString
        If PublicVariable.Connection.State = ConnectionState.Open Then
            PublicVariable.Connection.Close()
        End If
        Try
            PublicVariable.Connection.Open()
        Catch ex As Exception
            'MessageBox.Show(ex.Message)
            Return False
        End Try
        Return True
    End Function
    Public Sub LoadLanguage(ByVal frm As Form)
        Dim ControlName As String = ""
        Dim SubControlName As String = ""
        Dim Translate As String = ""
        Dim Origin As String = ""
        Dim Visible As Boolean = True
        Try
            frm.KeyPreview = True
            AddHandler frm.KeyUp, AddressOf KeyUp_New
            AddHandler frm.KeyDown, AddressOf KeyDown_New

            Dim mytable As DataTable = New DataTable()
            Dim arName As New ArrayList
            arName.Add("@frm")
            arName.Add("@LangCode")
            Dim arVal As New ArrayList
            arVal.Add(frm.Name)
            arVal.Add(PublicVariable.LangCode)

            mytable = BLL.ObjectGetBy("LANG_LOADBYFORM", arName, arVal)
            For i As Integer = 0 To mytable.Rows.Count - 1
                Dim ctrl As Control
                Origin = mytable.Rows(i).Item("Origin").ToString()
                ControlName = mytable.Rows(i).Item("ControlName").ToString()
                SubControlName = mytable.Rows(i).Item("SubControlName").ToString()
                Translate = mytable.Rows(i).Item("Translate").ToString()
                Visible = mytable.Rows(i).Item("Visible")
                If ControlName <> "" Then
                    ctrl = SystemFunction.GetControlByName(ControlName, frm)
                    If Not ctrl Is Nothing Then
                        If (TypeOf ctrl Is Janus.Windows.GridEX.GridEX) Then
                            If SubControlName <> "" Then
                                CType(ctrl, Janus.Windows.GridEX.GridEX).RootTable.Columns(SubControlName).Caption = Translate
                                CType(ctrl, Janus.Windows.GridEX.GridEX).RootTable.Columns(SubControlName).Tag = Origin
                                CType(ctrl, Janus.Windows.GridEX.GridEX).RootTable.Columns(SubControlName).Visible = Visible
                            End If
                            Dim grd As Janus.Windows.GridEX.GridEX
                            grd = ctrl
                            AddHandler grd.ColumnHeaderClick, AddressOf NewColumnClick_Event
                        ElseIf (TypeOf ctrl Is Janus.Windows.UI.Tab.UITab) Then
                            If SubControlName <> "" Then
                                CType(ctrl, Janus.Windows.UI.Tab.UITab).TabPages.Item(SubControlName).Text = Translate
                                CType(ctrl, Janus.Windows.UI.Tab.UITab).TabPages.Item(SubControlName).Tag = Origin
                            End If
                            Dim tab As Janus.Windows.UI.Tab.UITab
                            tab = ctrl
                            AddHandler tab.ChangingSelectedTab, AddressOf TAB_ChangingSelectedTab
                        Else
                            ctrl.Text = Translate
                            ctrl.Tag = Origin
                            ctrl.Visible = Visible
                            AddHandler ctrl.MouseDown, AddressOf NewDblClick_Event
                        End If
                    End If
                End If
            Next
            
        Catch ex As Exception
            MessageBox.Show(ControlName + SubControlName + ex.Message)
        End Try
    End Sub
    Private Sub TAB_ChangingSelectedTab(ByVal sender As System.Object, ByVal e As Janus.Windows.UI.Tab.TabCancelEventArgs)
        If PublicVariable.IsControl Then
            Dim a As Janus.Windows.UI.Tab.UITab
            a = sender
            If e.Page.Tag = "" Then
                e.Page.Tag = e.Page.Text
            End If
            If PublicVariable.IsConnect Then
                Dim frm As New frmLANGUAGE_CHANGE
                frm.lblOrigin.Text = e.Page.Tag
                frm.lblCurrent.Text = e.Page.Text
                If frm.ShowDialog() = DialogResult.OK Then
                    a.TabPages(e.Page.Key).Text = frm.txtNew.Text
                    SystemFunction.Update_Language(a.FindForm.Name, a.Name, e.Page.Key, frm.txtNew.Text, frm.lblOrigin.Text)
                End If
            End If
            PublicVariable.IsControl = False
        End If
    End Sub
    Private Sub NewColumnClick_Event(ByVal sender As System.Object, ByVal e As Janus.Windows.GridEX.ColumnActionEventArgs)
        If PublicVariable.IsControl Then
            Dim a As Janus.Windows.GridEX.GridEX
            a = sender
            If e.Column.Tag = "" Then
                e.Column.Tag = e.Column.Caption
            End If
            If PublicVariable.IsConnect Then
                Dim frm As New frmLANGUAGE_CHANGE
                frm.lblOrigin.Text = e.Column.Tag
                frm.lblCurrent.Text = e.Column.Caption
                If frm.ShowDialog() = DialogResult.OK Then
                    a.RootTable.Columns(e.Column.Key).Caption = frm.txtNew.Text
                    SystemFunction.Update_Language(a.FindForm.Name, a.Name, e.Column.Key, frm.txtNew.Text, frm.lblOrigin.Text)
                End If
            End If
            PublicVariable.IsControl = False
        End If
    End Sub
    Private Sub NewDblClick_Event(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        If PublicVariable.IsControl Then
            If sender.Tag = "" Then Return
            Dim a As Control
            a = sender
            If PublicVariable.IsConnect Then
                Dim frm As New frmLANGUAGE_CHANGE
                If a.Tag = "" Then
                    a.Tag = a.Text
                End If
                frm.lblOrigin.Text = a.Tag
                frm.lblCurrent.Text = a.Text
                If frm.ShowDialog() = DialogResult.OK Then
                    Dim NewText As String
                    NewText = frm.txtNew.Text
                    If NewText <> "" Then
                        sender.Text = NewText
                        SystemFunction.Update_Language(a.FindForm.Name, a.Name, "", NewText, frm.lblOrigin.Text)
                    End If
                End If
            End If
            PublicVariable.IsControl = False
        End If
          
    End Sub
    Private Sub KeyUp_New(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
        PublicVariable.IsControl = False
    End Sub
    Private Sub KeyDown_New(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs)
        If e.Control Then
            PublicVariable.IsControl = True
        End If
    End Sub

    Public Shared Function GetLanguageMessage(ByVal MessageCode As String) As String
        Dim dv As New DataView(PublicVariable.LangMessage)
        dv.RowFilter = "MessageCode='" + MessageCode + "'"
        If dv.Count > 0 Then
            Return dv.Item(0).Item("Translate").ToString
        Else
            Return ""
        End If

    End Function
    Public Shared Sub NewFormLoad(ByVal frm As Form)
        If frm.Width > 750 Then
            frm.StartPosition = FormStartPosition.Manual
            frm.Top = 0
            frm.Left = 0
            If PublicVariable.Screen = "1024" Then
                frm.Width = 1024
                frm.Height = 768
            End If
        End If
        Dim a As New PublicFunction
        a.LoadLanguage(frm)

    End Sub
    Public Shared Sub GRID_UP_DOWN(ByVal UP_DOWN As Integer, ByVal GRID As Janus.Windows.GridEX.GridEX)
        If GRID.RowCount = 0 Then Return
        Dim NoCardDisplay As Integer = GRID.Width / (GRID.CardWidth + GRID.CardSpacing)
        If NoCardDisplay < GRID.RowCount Then Return
        Dim CurrentPosition As Integer = GRID.Row
        If UP_DOWN = 1 Then ' UP
            If CurrentPosition + NoCardDisplay > GRID.RowCount Then Return
            GRID.Row = GRID.Row + NoCardDisplay
        Else ' DOWN
            If CurrentPosition < NoCardDisplay Then
                GRID.Row = 0
            End If
        End If
    End Sub
End Class
